home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #2 / Monster Media No. 2 (Monster Media)(1994).ISO / prog_c / pclc41.zip / MODEM_IO.C < prev    next >
Text File  |  1994-04-13  |  4KB  |  177 lines

  1. /*** MODEM_IO.C ***/
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "pcl4c.h"
  6. #include "ascii.h"
  7. #include "term.cfg"
  8. #include "modem_io.h"
  9.  
  10. #define FALSE 0
  11. #define TRUE !FALSE
  12. #define ONE_SECOND 18
  13.  
  14. /* NOTE: Requires AT COMMAND SET for all functions in this file */
  15.  
  16. #if AT_COMMAND_SET
  17.  
  18. #include <string.h>
  19. #include <ctype.h>
  20. #include "term_io.h"
  21.  
  22. int  BreakTest(void);
  23.  
  24. /*** send string to modem & get echo ***/
  25.  
  26. int SendTo(
  27.   int  Port,       /* port to talk to */
  28.   char *String)    /* string to send to modem */
  29. {int i;
  30.  char c;
  31.  int Code;
  32.  SioRxFlush(Port);
  33.  SioDelay(ONE_SECOND/4);
  34.  for(i=0;i<strlen(String);i++)
  35.     {/* User BREAK ? */
  36.      if(BreakTest()) return(FALSE);
  37.      /* fetch character */
  38.      c = toupper( String[i] );
  39.      switch(c)
  40.         {case '!':
  41.             /* replace ! with carriage return */
  42.             c = CR;
  43.             break;
  44.          case '~':
  45.             /* delay 1/2 second */
  46.             SioDelay(ONE_SECOND/2);
  47.             c = ' ';
  48.             break;
  49.          case ' ':
  50.             /* delay 1/4 second */
  51.             SioDelay(ONE_SECOND/4);
  52.             break;
  53.         } /* end switch */
  54.      /* transmit as 7 bit character */
  55.      PutChar(Port,(char)(0x7f & c));
  56.      /* delay 3/18th of a second */
  57.      SioDelay(3);
  58.      /* wait up to 1 second for the echo */
  59.      Code = GetChar(Port,ONE_SECOND);
  60.      /* echo char to display */
  61.      if(Code>0) SioCrtWrite((char)Code);
  62.     }
  63.  return(TRUE);
  64. } /* end SendTo */
  65.  
  66. void SayChar(char TheChar)
  67. {if((TheChar>=' ')&&(TheChar<='~')) printf("'%c'",TheChar);
  68.  else printf("%xH",TheChar);
  69. }
  70.  
  71. int GetModemChar(int Port, int Tics)
  72. {int Code;
  73.  /* get next char not a CR or LF */
  74.  while(1)
  75.    {Code = GetChar(Port,Tics);
  76.     if((Code>=0)&&((char)Code!=CR)&&((char)Code!=LF)) break;
  77.    }
  78.  return Code;
  79. }
  80.  
  81. /*** wait for incoming string from modem ***/
  82.  
  83. int WaitFor(
  84.   int  Port,     /* Port to talk to */
  85.   char *String,  /* string to wait for */
  86.   int  WaitTics) /* wait for 1st char */
  87. {int i, k;
  88.  char c;
  89.  int Code;
  90.  /* wait for string */
  91.  SioCrtWrite(CR);
  92.  SioCrtWrite(LF);
  93.  for(i=0;i<strlen(String);i++)
  94.    {/* User BREAK ? */
  95.     if(BreakTest()) return(FALSE);
  96.     c = String[i];
  97.     /* wait for next character */
  98.     if(i==0) Code = GetModemChar(Port,WaitTics);
  99.     else Code = GetModemChar(Port,ONE_SECOND);
  100.        /* printf("(\n%x)",Code);*/
  101.     if(Code==-1) return(FALSE);
  102.     SioCrtWrite((char)Code);
  103.     /* char must match */
  104.     if((char)Code != c)
  105.       {printf("\nExpecting "); SayChar(c);
  106.        printf(" not "); SayChar((char)Code);
  107.        printf(" at %d\n",i);
  108.        return(FALSE);
  109.       }
  110.    } /* end for(i) */
  111.  /* more characters ? */
  112.  while(1)
  113.    {Code = GetChar(Port,ONE_SECOND/2);
  114.     if(Code==-1) break;
  115.     SioCrtWrite((char)Code);
  116.    }
  117.  return(TRUE);
  118. } /* end WaitFor */
  119.  
  120. /*** enter command state ***/
  121.  
  122. /* NOTE: assumes escape char = '+' & guard time = 1 sec */
  123.  
  124. void CmdState(int Port)
  125. {int i;
  126.  /* delay a bit over 1 second */
  127.  SioDelay(ONE_SECOND+ONE_SECOND/4);
  128.  /* send Escape Code exactly 3 times */
  129.  for(i=0;i<3;i++)
  130.     {SioPutc(Port,'+');
  131.      SioDelay(ONE_SECOND/4);
  132.     }
  133.  /* delay again */
  134.  SioDelay(ONE_SECOND+ONE_SECOND/4);
  135. } /* end CmdState */
  136.  
  137. /*** hangup phone (in command state) ***/
  138.  
  139. void Hangup(int Port)
  140. {/* enter command state */
  141.  CmdState(Port);
  142.  /* hangup ! */
  143.  SendTo(Port,"!AT!");
  144.  SendTo(Port,"ATH0!");
  145. } /* end Hangup */
  146.  
  147. /*** dial & wait for CONNECT ***/
  148.  
  149. int DialPhone(
  150.   int  Port,     /* Port to talk to */
  151.   char *String)  /* string to wait for */
  152. {char Temp[80];
  153.  /* send dial string */
  154.  printf("...DIALING %s\n",String);
  155.  strcpy(Temp,"!ATDT");
  156.  strcat(Temp,String);
  157.  strcat(Temp,"!");
  158.  SendTo(Port,Temp);
  159.  /* wait up to 60 seconds for CONNECT */
  160.  if( WaitFor(Port,"CONNECT",60*ONE_SECOND) )
  161.    {/* send lone CR */
  162.     PutChar(Port,CR);
  163.     return TRUE;
  164.    }
  165.  return FALSE;
  166. } /* end DialPhone */
  167.  
  168. static int BreakTest(void)
  169. {/* User BREAK ? */
  170.  if(SioBrkKey())
  171.     {printf("User BREAK\n");
  172.      return(TRUE);
  173.     }
  174.  else return(FALSE);
  175. } /* end BreakTest */
  176. #endif
  177.